/**
 * Created by Administrator on 2017/12/25.
 */
var webpack = require('webpack');
var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin'); //css单独打包
var HtmlWebpackPlugin = require('html-webpack-plugin');
var webConfig = require('./src/config'); //服务配置
var publicPath = '/business/'; //服务器路径[发布]

var plugins = [];
// plugins.push(new webpack.optimize.UglifyJsPlugin({
//     compress: {
//         warnings: true,
//         drop_console: true
//     }
// }));
plugins.push(
    new webpack.DefinePlugin({
        'process.env': {
            'NODE_ENV': JSON.stringify('production')
        }
    })
);

plugins.push(new webpack.optimize.DedupePlugin());
plugins.push(new webpack.optimize.OccurenceOrderPlugin());
plugins.push(new ExtractTextPlugin('css/[name].css')); //css单独打包
plugins.push(new webpack.HotModuleReplacementPlugin()); //热替换
plugins.push(new HtmlWebpackPlugin({
    filename: '../business.html', //生成的html存放路径，相对于 path
    template: './src/Template/business.html', //html模板路径
    hash: true,
    favicon: __dirname + '/src/img/logo.ico',
    chunks: ['business', 'vendors'],
    inject: 'body'
}));


plugins.push(new webpack.optimize.CommonsChunkPlugin({
    name: 'vendors', // 将公共模块提取，生成名为`vendors`的chunk chunks: chunks, minChunks: chunks.length // 提取所有entry共同依赖的模块 }),
    minChunks: Infinity
}));


module.exports = {
    // devtool: '#source-map',     // 正确的输出代码行数
    entry: {
        business: './src/BusinessIndex'
    },
    output: {
        publicPath: publicPath, //编译好的文件，在服务器的路径
        path: path.join(__dirname, publicPath), //编译到当前目录
        filename: 'js/[name].js',
    },
    // externals: {
    //     'jquery': '$'
    // },
    devServer: { //服务代理
        port: webConfig.port,
        host: webConfig.getIP(),
        proxy: webConfig.getAgent()
    },
    module: {
        loaders: [{
            test: /\.js$/,
            loader: 'babel',
            query: {
                compact: false
            }
        }, {
            test: /\.css$/,
            loader: ExtractTextPlugin.extract('style-loader', 'css-loader!autoprefixer-loader')
        }, {
            test: /\.less$/,
            loader: ExtractTextPlugin.extract('style-loader', 'css-loader!autoprefixer-loader!less-loader')
        }, {
            test: /\.(scss|px)$/,
            loader: ExtractTextPlugin.extract('style-loader', 'css-loader!autoprefixer-loader!sass-loader')
        }, {
            test: /\.(eot|woff|svg|ttf|woff2|gif|appcache)(\?|$)/,
            loader: 'file-loader?name=[ext]/[name].[ext]'
        },
            {
                test: /\.(js|css|less|scss|jsx)$/,
                exclude: path.resolve(__dirname, "node_modules"),
                loader: 'webpack-px-to-rem',
                query: {
                    basePx: 24,
                    min: 1,
                    floatWidth: 3
                }
            },
            {
                test: /\.(png|jpg)$/,
                loader: 'url?limit=5000&name=img/[name][hash:5].[ext]' //注意后面那个limit的参数，当你图片大小小于这个限制的时候，会自动启用base64编码图片
            },
            {
                loader: "babel-loader",
                include: [
                    path.resolve(__dirname, "src"),
                ],
                test: /\.jsx?$/,
                query: {
                    plugins: ['transform-runtime', ['import', [{ libraryName: "antd", style: true }]]],
                    presets: ['es2015', 'stage-0', 'react'],

                }
            },
        ]
    },
    plugins: plugins,
    resolve: {
        extensions: ['', '.js', '.jsx'] //后缀名自动补全
    },
};
